package cn.yusei.teamTribe.controller.user;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.constant.TextType;
import cn.yusei.teamTribe.dao.user.po.UserInfoPO;
import cn.yusei.teamTribe.entity.UserInfo;
import cn.yusei.teamTribe.exception.SchoolNotFoundException;
import cn.yusei.teamTribe.exception.UserIdNotFoundException;
import cn.yusei.teamTribe.service.TextFilterService;
import cn.yusei.teamTribe.service.user.UserInfoService;
import cn.yusei.teamTribe.util.ResponseUtil;
import cn.yusei.teamTribe.util.StudentUtil;

@Controller
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private TextFilterService textFilterService;
	
	@RequestMapping(value="/userInfo/getById")
	public void getById(
			@RequestAttribute(value="id", required=false) Integer userId,
			@RequestParam(value="id") int id,
			PrintWriter out)
	{
		if(id <= 0)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		UserInfoPO userInfoPO = null;
		try
		{
			userInfoPO = userInfoService.get(id);
		}
		catch (UserIdNotFoundException e)
		{
			ResponseUtil.responseCode(out, ResponseCode.USER_NOT_EXISTS);
			return;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		UserInfo userInfo = new UserInfo();
		BeanUtils.copyProperties(userInfoPO, userInfo);
		if(userInfoPO.getSchoolId() != 0)
		{
			try
			{
				StudentUtil.poToStudent(userInfoPO.getStudentPO(), userInfo.getStudent());
			}
			catch (SchoolNotFoundException e)
			{
				e.printStackTrace();
				ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
				return;
			}
		}
		if(userId == null || userId != id)
			userInfo.updatePublic();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("user", userInfo);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value="/userInfo/update")
	public void update(@RequestAttribute("id") int  id, UserInfo userInfo, PrintWriter out)
	{
		userInfo.setId(id);
		userInfo.setNickname(textFilterService.getFilterString(userInfo.getNickname(), TextType.NICKNAME));
		if(!userInfo.isValid())
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		UserInfoPO userInfoPO = new UserInfoPO();
		BeanUtils.copyProperties(userInfo, userInfoPO);
		userInfoPO.setId(id);
		int code;
		try
		{
			code = userInfoService.update(userInfoPO);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
}
